package com.qf.controller;
import com.qf.Constant.AuthConstant;
import com.qf.Constant.RabbitMqConstant;
import com.qf.Constant.RedisConstant;
import com.qf.Utils.PassWordUtil;
import com.qf.entity.Msg;
import com.qf.entity.User;
import com.qf.feign.UserService;
import com.qf.result.Result;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/14/16:15
 * @Description:
 */
@Controller
@RequestMapping("user")
public class userController {
    @Autowired
    private UserService userService;
    @Autowired
 private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String,String> checkName(String param){
        User user = userService.getUserByName(param);
        Map<String,String> map=new HashMap<>();
        if (user==null){
        map.put("status","y");
        map.put("info","用户可用");
    }else {
            map.put("status","n");
            map.put("info","用户名已存在");
        }
        return map;
    }
    @RequestMapping("/sendSms/{phone}")
    @ResponseBody
    public void sendMsg(@PathVariable String phone){
        //letter:文本,numbers:数字
        System.out.println("手机号为:"+phone);

        String code = RandomStringUtils.random(6, false, true);
        redisTemplate.opsForValue().set(RedisConstant.AUTH_SMG_PRE+phone,code,60, TimeUnit.SECONDS);
        Msg msg = new Msg();
        msg.setCode(code);
        msg.setPhone(phone);
        rabbitTemplate.convertAndSend(RabbitMqConstant.SMS_EXCHANGE,"smg.send",msg);
        System.out.println("消息发送成功");
    }
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(User user){
        //校验验证码
        String code = user.getCode();
        String s = redisTemplate.opsForValue().get(RedisConstant.AUTH_SMG_PRE + user.getPhone());
        if(StringUtils.isEmpty(s)){
         return Result.error("验证码已过期");
        }
       if (!code.equals(s)){
         return   Result.error("验证码有误");
       }
       //对密码进行加密
       user.setPassword(PassWordUtil.hashpw(user.getPassword()));
       userService.addUser(user);
       return Result.success();
    }
    @RequestMapping("/login")
    @ResponseBody
    public Result login(User user, HttpServletRequest request){
       //查询用户是否存在
        String username = user.getUsername();
        User loginUser = userService.getUserByName(username);
        //检查用户名密码是否正确
        if(loginUser==null||!PassWordUtil.checkpw(user.getPassword(), loginUser.getPassword())){
            return Result.error("用户名或密码错误");
        }
        request.getSession().setAttribute(AuthConstant.AUTH_SESSION,loginUser);
        return Result.success();
    }
    @RequestMapping("loginOut")
    public String loginOut(HttpSession httpSession){
        httpSession.invalidate();
        return "redirect:http://localhost/auth/login.html";
    }
}
